var RecondTableList = {// 通话记录初始化表格
    id: "RecondTableList",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

function _cellStyles(value, row, index) {
    if (row.result == 2) {
        return {
            css: {
                "color": "#ff0000",
                "overflow": "hidden",
                "white-space": "nowrap",
                "text-overflow": "ellipsis"
            }
        }
    } else {
        return {css: {"color": "#008000", "overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    }
}

RecondTableList.initColumn = function () {//初始化表格的列
    var columns = [
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'remarks', field: 'remarks', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'isexistfile', visible: false, align: 'center', valign: 'middle'},
        {
            title: '类型', field: 'bm', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '开始时间', field: 'beginTime', align: 'center', valign: 'middle', sortable: true, width: 90,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '时长', field: '', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return intervalTime(row.beginTime, row.endTime);
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '状态', field: 'result', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                if (value == 0) {
                    return '已接'
                } else if (value == 2) {
                    return '未接'
                }else if (value == 4) {
                    return "<a href='javascript:;' style='color: green;display: flex;align-items: center;' onclick = 'RecondTableList.result4(" + '"' + row.remarks + '"' + ")' >已处理</span></a>"
                } else {
                    return '未接'
                }
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '操作', field: '', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                if (row.bm == "呼出") {
                    if (row.isexistfile == 1) {
                        return '<button title="录音记录" onclick="RecondTableList.play(' + "'" + row.id + "'" + ')"  style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0 5px;" type="button" class="btn"><span class="glyphicon glyphicon-headphones"></span></button> '
                    } else {
                        return "";
                    }
                }
                if (row.bm == "呼入") {
                    if (row.isexistfile == 1) {
                        return '<button title="录音记录" onclick="RecondTableList.play1(' + "'" + row.id + "'" + ')"  style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0 5px;" type="button" class="btn"><span class="glyphicon glyphicon-headphones"></span></button> '
                    } else {
                        return "";
                    }
                }
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        }
    ];
    return columns;
};

function intervalTime(startTime, endTime) {
    //只需要时分秒
    var date1 = new Date(startTime);  //开始时间
    var date2 = new Date(endTime);    //结束时间
    var date3 = date2.getTime() - date1.getTime();  //时间差的毫秒数
    //计算小时
    var hours = Math.floor(date3 / (3600 * 1000));
    //计算小时后剩余的毫秒数
    var leave1 = date3 % (3600 * 1000);
    //计算相差分钟数
    var minutes = Math.floor(leave1 / (60 * 1000));
    //计算分钟数后剩余的毫秒数
    var leave2 = leave1 % (60 * 1000);
    //计算妙数
    var seconds = Math.round(leave2 / 1000);
    return hours + "小时 " + minutes + " 分钟" + seconds + " 秒";
}

RecondTableList.play = function (id) {//点击表格展示详情
    var index = layer.open({
        type: 2,
        title: '播放录音',
        area: ['40%', '30%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservationsNotArrive/play/' + id
    });
    this.layerIndex = index;
};

RecondTableList.result4 = function (remarks) {//点击表格展示详情
   alert(remarks);
};

RecondTableList.play1 = function (id) {//点击表格展示详情
    var index = layer.open({
        type: 2,
        title: '播放录音',
        area: ['40%', '30%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservationsNotArrive/play1/' + id
    });
    this.layerIndex = index;
};
$(function () {
    var customerPhone = $("#customerPhone").val();
    var defaultColunms = RecondTableList.initColumn();
    var table = new BSTable(RecondTableList.id, "/reservationsCallRecord/callRecordlist/" + customerPhone, defaultColunms);
    table.setPaginationType("client");
    RecondTableList.table = table.init();
});